<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序号" prop="sortNo">
				<el-input-number v-model="form.sortNo" controls-position="right" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="设备名称" prop="deviceName">
				<el-input v-model="form.deviceName" placeholder="设备名称" maxlength="48" clearable></el-input>
			</el-form-item>
			<el-form-item label="设备编号" prop="deviceCode">
				<el-input v-model="form.deviceCode" placeholder="设备编号" maxlength="48" clearable></el-input>
			</el-form-item>
			<el-form-item label="设备品牌" prop="brand">
				<el-input v-model="form.brand" placeholder="设备品牌" maxlength="48" clearable></el-input>
			</el-form-item>
			<el-form-item label="规格型号" prop="model">
				<el-input v-model="form.model" placeholder="规格型号" maxlength="48" clearable></el-input>
			</el-form-item>
			<el-form-item label="管理员" prop="manager">
				<select-user-dialog v-model:accountId="form.manager" :mode="false"/>
			</el-form-item>
			<el-form-item label="开放范围" prop="deptRole">
				<select-dept-dialog v-model:deptId="form.deptRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-switch v-model="form.status" active-value="0" inactive-value="1"></el-switch>
			</el-form-item>
			<el-form-item label="备注">
				<el-input type="textarea" :rows="2" maxlength="280" placeholder="备注" v-model="form.remark"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
export default {
	components: {
		selectUserDialog,
		selectDeptDialog
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增会议室',
				edit: '编辑会议室'
			},
			form: {
				deviceId: "",
				deviceCode:"",
				deviceName: "",
				sortNo:"",
				brand: "",
				model:'',
				manager:'',
				deptRole:'',
				remark: "",
				status: "0"
			},
			rules: {
				deviceName: [
					{required: true, message: '设备名称不能为空'}
				],
				deviceCode: [
					{required: true, message: '设备编号不能为空'}
				],
				manager: [
					{required: true, message: '请指定设备管理员'}
				],
				sortNo: [
					{required: true, message: '请输入排序', trigger: 'change'}
				],
			},
			visible: false,
			isSaveing: false,
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					var res;
					if(this.mode=="add")
					{
						this.isSaveing = true;
						res = await this.$API.office.meeting.insertMeetingDevice.post(this.form);
						this.isSaveing = false;
					}else if(this.mode=="edit")
					{
						this.isSaveing = true;
						res = await this.$API.office.meeting.updateMeetingDevice.post(this.form);
						this.isSaveing = false;
					}
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.deviceId = data.deviceId
			this.form.deviceCode = data.deviceCode
			this.form.sortNo = data.sortNo
			this.form.deviceName = data.deviceName
			this.form.brand = data.brand
			this.form.model = data.model
			this.form.manager = data.manager
			this.form.remark = data.remark
			this.form.status = data.status
		}
	}
}
</script>

<style>
</style>
